/**
 * axios基础构建
 * @date 2019-12-24
 */

import getUrl from './config';
import instance from './intercept';
import type {AxiosRequest, CustomResponse} from './types';
import {getToken} from '@/unils/token';

class Abstract {
    // 外部传入的baseUrl
    // protected baseURL: string = 'http://127.0.0.1:6633';
    protected baseURL: string = 'https://prvideord.axian.fun/';
    // protected baseURL: string =
    //   process.env.envType === 'dev' ? '/api' : 'https://technique-rd.axian.fun/';
    // 自定义header头
    headers = () => {
        return {
            // contentType: 'application/x-www-form-urlencoded',
            // ContentType: 'application/json;charset=UTF-8',
            ContentType: 'application/json',
            Authorization: getToken(),
        };
    };

    /**
     * GET类型的网络请求
     */
    protected getReq({
                         baseURL,
                         headers,
                         url,
                         data,
                         params,
                         responseType,
                     }: AxiosRequest) {
        return this.apiAxios({
            baseURL,
            headers,
            method: 'GET',
            url,
            data,
            params,
            responseType,
        });
    }

    /**
     * POST类型的网络请求
     */
    protected postReq({
                          baseURL,
                          headers,
                          url,
                          data,
                          params,
                          responseType,
                      }: AxiosRequest) {
        return this.apiAxios({
            baseURL,
            headers,
            method: 'POST',
            url,
            data,
            params,
            responseType,
        });
    }

    /**
     * PUT类型的网络请求
     */
    protected putReq({
                         baseURL,
                         headers,
                         url,
                         data,
                         params,
                         responseType,
                     }: AxiosRequest) {
        return this.apiAxios({
            baseURL,
            headers,
            method: 'PUT',
            url,
            data,
            params,
            responseType,
        });
    }

    /**
     * DELETE类型的网络请求
     */
    protected deleteReq({
                            baseURL,
                            headers,
                            url,
                            data,
                            params,
                            responseType,
                        }: AxiosRequest) {
        return this.apiAxios({
            baseURL,
            headers,
            method: 'DELETE',
            url,
            data,
            params,
            responseType,
        });
    }

    private apiAxios({
                         baseURL = this.baseURL,
                         headers = this.headers(),
                         method,
                         url,
                         data,
                         params,
                         responseType,
                     }: AxiosRequest): Promise<CustomResponse> {
        // url解析
        const _url = (url as string).split('.');
        url = getUrl(_url[0], _url[1]);

        return new Promise((resolve, reject) => {
            instance({
                baseURL,
                headers,
                method,
                url,
                params,
                data,
                responseType,
            })
                .then((res: any) => {
                    // 200:服务端业务处理正常结束
                    if (res.data.code === 0) {
                        resolve({
                            status: true,
                            message: res.data.message,
                            data: res.data?.data,
                            origin: res.data,
                        });
                    } else {
                        resolve({
                            status: false,
                            message: res.data?.msg || url + '请求失败',
                            data: res.data?.data,
                        });
                    }
                })
                .catch((err: any) => {
                    const message = err?.data?.msg || err?.message || url + '请求失败';
                    // eslint-disable-next-line
                    reject({status: false, message, data: null});
                });
        });
    }
}

export default Abstract;
